<template>
  <div class="channel-edit">
    <van-cell title="我的频道" :border='false'>
      <van-button plain type="primary" size="small" round class="edit-btn" color="red" @click="isEdit = !isEdit">
        {{isEdit ? '完成' : '编辑'}}
      </van-button>
    </van-cell>
    <van-grid
      :border='false'
      :gutter="10"
      class="my-grid"
      >
      <van-grid-item
        :class="{ active : index === active }"
        :text="item.name"
        :icon="isEdit && index !== 0 ? 'close' : ''"
        v-for="(item,index) in channels"
        :key="item.id"
        @click="onMyClick(index,item.id)"
      />
    </van-grid>
    <van-cell title="我的频道" :border='false'></van-cell>
    <van-grid :border='false' :gutter="10" class="my-grid" direction="horizontal" icon-size='15px'>
      <van-grid-item icon="plus" :text="item.name" v-for="item in recommendChannels" :key="item.id" @click="onRecommendChannel(item)"/>
    </van-grid>
  </div>
</template>

<script>
import { getAllChannel } from '@/api/article'
import { Toast } from 'vant'
import { mapState } from 'vuex'
import { setStorage } from '@/utils/storage'
import { addUserChannelItem, removeUserChannelItem } from '@/api/user'
export default {
  props: {
    channels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      isEdit: false,
      allChannels: []
    }
  },
  computed: {
    ...mapState(['user']),
    recommendChannels () {
      // 对allChannels进行筛选
      // allChannels的每一项 是否在channels里，如果没有在channels，属于recommendChannels
      // const arr = this.allChannels.filter(item => {
      //   const flag = this.channels.some(item2 => {
      //     return item2.id === item.id
      //   })
      //   return !flag
      // })
      // return arr
      return this.allChannels.filter(item => {
        return !this.channels.some(item2 => item2.id === item.id)
      })
    }
  },
  created () {
    this.loadAllChannel()
  },

  methods: {
    onMyClick (index, id) {
      if (this.isEdit) {
        if (index === 0) return
        if (index <= this.active) {
          this.$emit('setActive')
        }
        this.channels.splice(index, 1)
        console.log('删除')
        // 删除持久化
        this.deleteChannelStorage(id)
      } else {
        console.log('跳转')
        this.$emit('onGo', index)
      }
    },
    // 删除持久化
    async deleteChannelStorage (id) {
      // 未登录
      if (!this.user) return setStorage('MY-CHANNELS', this.channels)
      // 已登录
      Toast.loading({
        message: '删除中...',
        forbidClick: true
      })
      try {
        await removeUserChannelItem(id)
        console.log(id)
        Toast.success('删除成功')
      } catch (err) {
        Toast.fail('删除失败')
      }
    },
    async loadAllChannel () {
      try {
        const res = await getAllChannel()
        this.allChannels = res.channels
      } catch (err) {
        Toast('获取失败')
      }
    },
    async onRecommendChannel (obj) {
      this.channels.push(obj)
      Toast.loading({
        message: '加载中...',
        forbidClick: true
      })
      if (this.user) {
        try {
          await addUserChannelItem({
            id: obj.id,
            seq: this.channels.length
          })
          Toast.success('添加成功')
        } catch (err) {
          Toast.fail('添加失败')
        }
      } else {
        setStorage('MY-CHANNELS', this.channels)
      }
    }
  }
}
</script>

<style scoped lang='less'>
.channel-edit {
  .edit-btn {
    width: 104px;
    height: 48px;
  }
  /deep/.my-grid {
    .van-grid-item {
      width: 160px;
      height: 86px;
      border-radius: 6px;
      }
    .active {
      .van-grid-item__text {
          color: red;
        }
    }
    .van-grid-item__content {
      background: #F4F5F6;
    }
    .van-icon-close {
      font-size: 35px;
      position: absolute;
      top: -10.5px;
      right: 5px;
    }
    .van-grid-item__icon+.van-grid-item__text {
      margin-top: 0;
    }

  }
}
</style>
